package com.bestescortbedsystem.service.impl;

import com.bestescortbedsystem.dao.*;
import com.bestescortbedsystem.entity.*;
import com.bestescortbedsystem.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: LuXi
 * @Date: 2021/3/1 16:57
 * @Description:
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private SesUserDao sesUserDao;
    @Resource
    private SesUserOrderDao sesUserOrderDao;
    @Resource
    private SesBedMessageDao sesBedMessageDao;
    @Resource
    private SesBedTypeDao sesBedTypeDao;
    @Resource
    private SesHospitalinfoDao sesHospitalinfoDao;
    @Resource
    private SesHospitalinfoDepartmentDao sesHospitalinfoDepartmentDao;
    @Override
    public PageInfo<SesUserOrder> info(OrderSearch orderSearch, Integer pageSize, Integer pageNum) {
        //查询
        SesUserOrderExample orderExample = new SesUserOrderExample();
        SesUserOrderExample.Criteria orderCriteria = orderExample.createCriteria();
        //判断查询范围
        if(orderSearch.getFlag() != null){
            if (orderSearch.getFlag()) {
                System.out.println("*********true**********");
                if (orderSearch.getUserordernum() != null && !orderSearch.getUserordernum().equals("")) {
                    orderCriteria.andUserordernumEqualTo(orderSearch.getUserordernum());
                }
            }else{//其他条件查询
                System.out.println("*********false**********");
                //医院查询
                if(orderSearch.getHospital() != null && orderSearch.getHospital() != 0){
                    //获得医院对象
                    SesHospitalinfo hospital = sesHospitalinfoDao.selectByPrimaryKey(orderSearch.getHospital());
                    //查找拥有该医院Id的床位
                    SesBedMessageExample bedMessageExample = new SesBedMessageExample();
                    SesBedMessageExample.Criteria bedCriteria = bedMessageExample.createCriteria();
                    bedCriteria.andHospitalinfoidEqualTo(hospital.getHospitalinfoid());
                    List<SesBedMessage> beds = sesBedMessageDao.selectByExample(bedMessageExample);
                    //获得拥有该床位的订单
                    List<Long> ids = new ArrayList<>();
                    for (SesBedMessage sbm:beds) {
                        ids.add(sbm.getBedmessageid());
                    }
                    orderCriteria.andBedMessageidIn(ids);
                }

                //科室
                if(orderSearch.getDepartment() != null && orderSearch.getDepartment() != 0){
                    //获得科室对象
                    SesHospitalinfoDepartment department = sesHospitalinfoDepartmentDao.selectByPrimaryKey(orderSearch.getDepartment());
                    //查找拥有该科室Id的床位
                    SesBedMessageExample bedMessageExample = new SesBedMessageExample();
                    SesBedMessageExample.Criteria bedCriteria = bedMessageExample.createCriteria();
                    bedCriteria.andHospitalinfoDepartmentidEqualTo(department.getDepartmentid());
                    List<SesBedMessage> beds = sesBedMessageDao.selectByExample(bedMessageExample);
                    //获得拥有该床位的订单
                    List<Long> ids = new ArrayList<>();
                    for (SesBedMessage sbm:beds) {
                        ids.add(sbm.getBedmessageid());
                    }
                    orderCriteria.andBedMessageidIn(ids);
                }
                //状态
                if(orderSearch.getUserorderstatus() != null && orderSearch.getUserorderstatus() !=0){
                    orderCriteria.andUserorderstatusEqualTo(orderSearch.getUserorderstatus());
                }

                //时间
                if((orderSearch.getTimeStart() != null && !orderSearch.getTimeStart().equals(""))&&(orderSearch.getTimeEnd() != null && !orderSearch.getTimeEnd().equals(""))){
                    //全部存在
                    orderCriteria.andUserorderstarttimeBetween(orderSearch.getTimeStart(),orderSearch.getTimeEnd());
                }else{
                    //单个存在
                    if(orderSearch.getTimeStart() != null && !orderSearch.getTimeStart().equals("")){
                        orderCriteria.andUserorderstarttimeGreaterThanOrEqualTo(orderSearch.getTimeStart());
                    }
                    if(orderSearch.getTimeEnd() != null && !orderSearch.getTimeEnd().equals("")){
                        orderCriteria.andUserorderstarttimeLessThanOrEqualTo(orderSearch.getTimeEnd());
                    }
                }
            }
        }

        //全部订单
        //分页
        Integer size = sesUserOrderDao.selectByExample(orderExample).size();
        PageHelper.startPage(pageNum,pageSize);
        List<SesUserOrder> list = sesUserOrderDao.selectByExample(orderExample);
        list.get(0).setSize(size);
        //获取订单其他信息
        for (SesUserOrder so : list) {
            //获得电话
            Long userphone = sesUserDao.selectByPrimaryKey(so.getUserUserid()).getUserphone();
            so.setPhone(userphone);
            //获得陪护床对象
            SesBedMessage bedMessage = sesBedMessageDao.selectByPrimaryKey(so.getBedMessageid());
            try {
                //获得医院与科室Id
                Long hid = bedMessage.getHospitalinfoid();
                Long did = bedMessage.getHospitalinfoDepartmentid();
                String hname = sesHospitalinfoDao.selectByPrimaryKey(hid).getHospitalinfoname();
                String dname = sesHospitalinfoDepartmentDao.selectByPrimaryKey(did).getDepartmentname();
                so.setHospital(hname);
                so.setDepartment(dname);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        PageInfo<SesUserOrder> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public List<SesHospitalinfo> hospitalInfo() {
        return sesHospitalinfoDao.selectByExample(null);
    }

    @Override
    public List<SesHospitalinfoDepartment> departmentInfo() {
        return sesHospitalinfoDepartmentDao.selectByExample(null);
    }

    /**
     * 删除订单
     * @param userorderid
     */
    @Override
    public void deleteOrder(Long userorderid) {
        sesUserOrderDao.deleteByPrimaryKey(userorderid);
    }

    /**
     * 获取单个订单详情
     * @param userorderid
     * @return
     */
    @Override
    public Map<String, Object> orderXiangQing(Long userorderid) {
        Map<String, Object> map = new HashMap<>();
        //获得订单
        SesUserOrder order = sesUserOrderDao.selectByPrimaryKey(userorderid);
        map.put("order",order);
        //获得床位信息
        SesBedMessage bed = sesBedMessageDao.selectByPrimaryKey(order.getBedMessageid());
        map.put("bed",bed);
        //获得医院与科室
        SesHospitalinfo hospital = sesHospitalinfoDao.selectByPrimaryKey(bed.getHospitalinfoid());
        SesHospitalinfoDepartment department = sesHospitalinfoDepartmentDao.selectByPrimaryKey(bed.getHospitalinfoDepartmentid());
        map.put("hospital",hospital);
        map.put("department",department);
        //获得陪护床设备信息
        SesBedType bedType = sesBedTypeDao.selectByPrimaryKey(bed.getBedBedTypeid());
        map.put("bedType",bedType);
        //获得用户信息
        SesUser user = sesUserDao.selectByPrimaryKey(order.getUserUserid());
        map.put("user",user);
        return map;
    }
}
